Write-Host -ForegroundColor yellow "Please  put the folder path and make sure in every line 
these is no space  in leading and trailing before saving it"
clear-content .\folder.txt
clear-content .\errorfolder.txt
invoke-item .\folder.txt
read-host " "
$exceptgrp = cat .\ExceptGroups.txt #不需要跑的那些组
$folders = Get-Content .\folder.txt
$erroractionpreference = "stop"
get-date
#<-----------------------------------------Part1 Test Folder Path Existence
if ($folders -eq $null){
Write-Host -ForegroundColor red "No folder is saved in the text file"
break}
else {
	foreach ($folder in $folders)
	{
		if ((test-path $folder) -eq $false)
		{echo $folder >>.\errorfolder.txt}
	}
	if ((cat .\errorfolder.txt).Length -gt 0){
	Write-Host -ForegroundColor yellow "Please double check the folders if there is anyone in the opened errorfolder txt"
	invoke-item .\errorfolder.txt
	read-host " "}
}
#<-------------Part2 Generate Folder Group Info and  Inheritance Status
clear-content .\folder.csv
clear-content .\GroupMember.csv
function Get-GroupHierarchy ($searchGroup)
{
$GroupName=(get-adgroup $searchGroup -properties Name).Name
$groupMember = get-adgroupmember $searchGroup| sort-object objectClass -descending
	if ($groupMember -eq $null){
		$Properties = @{Inheritance=$Inheritance;Filename = $folder;GroupName=$GroupName;GroupAccess=$groupaccess;Name="No GroupMembers"}
		$Newobject = New-Object  PSObject -Property  $Properties
		$script:Array +=$Newobject
		}
	else{
	   foreach ($member in $groupMember)
		{
		if ($member.objectclass -eq "user")
			{
			$userinfo = get-Aduser $member.samaccountname -Properties *
			$LLD=$userinfo.LastLogonDate
			if($LLD -ne $null){$LLD="+"+$userinfo.LastLogonDate.ToString("MM/dd/yyyy HH:mm:ss")}
			else{$LLD="N/A"}
			$Properties = @{Inheritance=$Inheritance;Filename = $folder;GroupName=$GroupName;GroupAccess=$groupaccess;Name=$member.Name;ID=$member.samaccountname;Description=$userinfo.Description; Enabled=$Userinfo.Enabled; LastLogonDate=$LLD}
			$Newobject = New-Object  PSObject -Property  $Properties
			$script:Array +=$Newobject
			}
		if ($member.objectclass -eq "group" -and $script:subGrps -notcontains  $member.samaccountname)
			{
				if((Get-PSCallStack).Count -lt 999){
					$script:subGrps+=$member.samaccountname
					Get-GroupHierarchy $member.samaccountname
				}
			}
		}
	}
}
foreach ($folder in Get-Content .\folder.txt)
{
	Get-NTFSAccess -Path  $folder | Select-Object -Property FullName, Account, AccessRights, InheritanceEnabled, IsInherited |ConvertTo-CSV -NoTypeInformation|Add-Content .\folder.csv
}
$erroractionpreference = "SilentlyContinue"
$num = 1
foreach ($folder in get-content .\folder.txt)
{
wh -foregroundcolor "yellow" "Processing on $num folder $folder"
$FolderAccess = Get-NTFSAccess $folder
$Inheritance = $FolderAccess[0].InheritanceEnabled
foreach($item in $FolderAccess){
	$group = $item.Account.Tostring().Split("\")[1]
	if ($exceptgrp -notcontains $group )
		{if(get-adgroup $group)
			{
			$script:array = @()
			$script:subGrps=@()
			$script:subGrps+=$exceptgrp
			$script:subGrps+=$group

			if($item.InheritanceFlags.ToString() -eq "ContainerInherit"){
				$groupaccess = "List folder contents"
			}else{
				$groupaccess = $item.accessrights
			}
			Get-GroupHierarchy $group 
			$script:array | Select-Object Filename,Inheritance,GroupName,GroupAccess,Name,ID,Description,Enabled,LastLogonDate| Export-CSV .\temp.csv -UseCulture -NoTypeInformation -Encoding UTF8
			get-content .\temp.csv  | Add-Content .\GroupMember.csv
			clear-content .\temp.csv
			} 
		}
	}
$num=$num+1
}
get-date
#Start-Process -FilePath "C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE"
Send-MailMessage -from $EAMail -to $NotesMail -subject "Network Folder Report" -Attachments .\folder.csv, .\GroupMember.csv -smtpserver xxxx -Encoding ([System.Text.Encoding]::UTF8)
